<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH."third_party/MX/Controller.php";

class MY_Controller extends MX_Controller
{	
	public $adodb;
	protected $db_var;
	protected $debug;
	public $crud;
	public $icon;
	
	function __construct()
	{
		parent::__construct();
		require_once(APPPATH."libraries/adodb/adodb.inc.php");	
		include_once(APPPATH."libraries/Grocery_CRUD.php");
		
		$this->load->library('Adodb');
		$this->load->library('DB_Cache');
		
		$this->load->database();
		$dsn = $this->db->dbdriver.'://'.$this->db->username.':'.$this->db->password.'@'.$this->db->hostname.'/'.$this->db->database;		
		$this->adodb =& ADONewConnection($dsn);
		
		if( $this->db_var ){
			$this->db =& $this->adodb;
		}
		
		if( $this->debug ){
			$this->adodb->debug = TRUE;	
		}
		
		$this->get_config();
		$this->set_icon();
		//$this->db->cache_on();
		//$this->output->enable_profiler(TRUE);
		
		$this->crud = new grocery_CRUD();
		$this->crud->set_theme('twitter-bootstrap');
		$this->crud->unset_export();
		$this->crud->unset_print();
		$this->crud->unset_delete_multiple();
	}
	
	protected function get_config()
	{
		$result = $this->adodb->GetAll("SELECT * FROM sys_config");
		
		foreach((array)$result as $config){
			$conf[$config['category']][$config['name']] = $config['value'];
		}
		
		foreach($conf as $name=>$value){
			(object)$this->config->$name = $value;	
		}		
		

		$this->config->meta['og']['title'] 			= $this->config->meta['title'];
		$this->config->meta['og']['url'] 			= _URL;
		$this->config->meta['og']['site_name']		= $this->config->meta['site_name'];
		$this->config->meta['og']['image'] 			= _URL."files/logo/".$this->config->meta['logo'];
		$this->config->meta['og']['type'] 			= "article";
		$this->config->meta['og']['description'] 	= $this->config->meta['description'];		
	}
	
	protected function set_icon()
	{
		$this->load->config('glyphicon');
		$this->load->config('font_awesome');
		
		$glyphicon = $this->config->item('glyphicon');
		$fontawesome = $this->config->item('fa');
		
		foreach((array)$glyphicon as $key=>$val)
		{
			$this->icon->glyphicon[$key] = "<i class=\"glyphicon $val\"></i>";	
		}
		
		foreach((array)$fontawesome as $k=>$v)
		{
			$this->icon->fa[$k] = "<i class=\"fa $v\"></i>";	
		}
	}
	
	public function set_log($str)
	{
		$this->load->library('user_agent');
		
		$now 		= date('Y-m-d H:i:s');
		$thismonth 	= date('m');
		$thisyear 	= date('Y');
		$tablename 	= "sys_log_".$thismonth."_".$thisyear;
		
		$data = array();
		$data['datetime'] 	= time();
		$data['action'] 	= strip_tags($str);
		$data['ip']			= $this->input->ip_address();
		$data['browser']	= $this->agent->browser();
		$data['platform']	= $this->agent->agent_string();
		$data['url']		= $this->uri->uri_string();	
		$data['postdata']	= isset( $_POST ) ? $this->encrypt(json_encode($_POST)) : "";	
		
		$is_exists = $this->adodb->GetOne("SHOW TABLES LIKE '$tablename'");
		if( $is_exists ){
			$this->adodb->AutoExecute($tablename,$data,"INSERT");
		}else{
			$query = "CREATE TABLE IF NOT EXISTS `$tablename` (
						  `id` int(11) NOT NULL AUTO_INCREMENT,
						  `datetime` varchar(255) DEFAULT NULL,
						  `action` text,
						  `ip` varchar(255) DEFAULT NULL,
						  `browser` text,
						  `url` text,
						  `platform` text,
						  `postdata` text,
						  PRIMARY KEY (`id`)
						) ENGINE=InnoDB  DEFAULT CHARSET=latin1";
			$create_table = $this->adodb->Execute($query);
			if($create_table){
				$this->adodb->AutoExecute($tablename,$data,"INSERT");
			}
		}
	}
	
	public function encrypt($plain_text, $password = 'OuzsPh0dq8', $iv_len = 16)
	{
	   $plain_text .= "\2008A";
	   $n = strlen($plain_text);
	   if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
	   $i = 0;
	   $enc_text = $this->getRandomCode($iv_len);
	   $iv = substr($password ^ $enc_text, 0, 512);
	   while ($i < $n) {
		   $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv));
		   $enc_text .= $block;
		   $iv = substr($block . $iv, 0, 512) ^ $password;
		   $i += 16;
	   }
	   return base64_encode($enc_text);
	}
	
	
	public function decrypt($enc_text, $password = 'OuzsPh0dq8', $iv_len = 16)
	{
	   $enc_text = base64_decode($enc_text);
	   $n = strlen($enc_text);
	   $i = $iv_len;
	   $plain_text = '';
	   $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
	   while ($i < $n) {
		   $block = substr($enc_text, $i, 16);
		   $plain_text .= $block ^ pack('H*', md5($iv));
		   $iv = substr($block . $iv, 0, 512) ^ $password;
		   $i += 16;
	   }
	   return preg_replace('/\\2008A\\x00*$/', '', $plain_text);
	}
	
	public function getRandomCode($iv_len)
	{
	   $iv = '';
	   while ($iv_len-- > 0) {
		   $iv .= chr(mt_rand() & 0xff);
	   }
	   return $iv;
	}
}

include_once(APPPATH . "libraries/Admin_Controller.php");
include_once(APPPATH . "libraries/Public_Controller.php");